package com.linked.basemanage.permissioncontrol.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.permissioncontrol.bean.param.AddPowersToRoleParam;
import com.linked.basemanage.permissioncontrol.bean.param.AddRolesToUserParam;
import com.linked.basemanage.permissioncontrol.bean.po.PowerInfoPO;
import com.linked.basemanage.permissioncontrol.bean.po.RoleInfoPO;
import com.linked.basemanage.permissioncontrol.service.IRoleInfoService;
import com.linked.basemanage.permissioncontrol.service.IRolePowersService;
import com.linked.basemanage.permissioncontrol.service.IUserRolesService;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :dbq
 * @date : 2023/2/22 14:46
 * @description : desc
 */
@RestController
@Slf4j
@RequestMapping("roleinfo")
public class RoleInfoController {

    private final ObjectMapper mapper;

    private final IRoleInfoService roleInfoService;

    private final IRolePowersService rolePowersService;

    private final IUserRolesService userRolesService;

    @Autowired
    public RoleInfoController(ObjectMapper mapper, IRoleInfoService roleInfoService, IRolePowersService rolePowersService, IUserRolesService userRolesService) {
        this.mapper = mapper;
        this.roleInfoService = roleInfoService;
        this.rolePowersService = rolePowersService;
        this.userRolesService = userRolesService;
    }

    @PostMapping("addRole")
    Result addRole(@RequestBody RoleInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("新增角色接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = roleInfoService.addRole(param);
        } catch (Exception e) {
            log.error("新增角色接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    @PostMapping("addPowersToRole")
    Result addPowersToRole(@RequestBody AddPowersToRoleParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("给角色加权限接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = rolePowersService.addPowersToRole(param);
        } catch (Exception e) {
            log.error("给角色加权限接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    @PostMapping("addRolesToUser")
    Result addRolesToUser(@RequestBody AddRolesToUserParam param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("给用户加角色接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = userRolesService.addRolesToUser(param);
        } catch (Exception e) {
            log.error("给用户加角色接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

}
